/**
 * Copyright (c) 2012 Fujian FUNO Mobile Communication Technology Co., Ltd.
 * All rights reserved by FUNO.
 */
package com.funo.ehealth.util;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/** 
 * http工具类
 * 
 * @version V1.1
 * @author zhangguangying
 * @date 2012-11-15
 */
public class HttpUtil {
	
	/**
	 * HttpUtil日志记录器
	 */
	private static final Log log = LogFactory.getLog(HttpUtil.class);
	
	/**
	 * http连接
	 */
	private HttpURLConnection connection = null;
	/**
	 * http写出流
	 */
	private DataOutputStream out = null;
	/**
	 * http读入流
	 */
	private BufferedReader reader = null;

	/**
	 * http连接请求
	 * @param httpUrl url地址
	 * @param connectTimeout 连接超时时间
	 * @param readTimeout 读取超时时间
	 * @param doOutput 是否可写
	 * @param doInput 是否可读
	 * @param requestMethod 请求方式
	 * @return HttpURLConnection
	 * @throws IOException 
	 */
	public void connect(String httpUrl,int connectTimeout,int readTimeout,boolean doOutput,boolean doInput,String requestMethod) throws IOException{
		try {
			URL url = new URL(httpUrl); 
			connection = (HttpURLConnection) url.openConnection();
			connection.setConnectTimeout(connectTimeout); 
			connection.setReadTimeout(readTimeout);  
			connection.setDoOutput(doOutput);
			connection.setDoInput(doInput);
			connection.setRequestMethod(requestMethod); 
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
			connection.connect();
		} catch (IOException e) {
//			LogUtil.error(log,e,"http连接请求异常，参数:{httpUrl:{},connectTimeout:{},readTimeout:{},doOutput:{},doInput:{},requestMethod:{}}", httpUrl,connectTimeout,readTimeout,doOutput,doInput,requestMethod);
			throw new IOException(StringUtil.handleString("http连接请求异常，参数:{httpUrl:{},connectTimeout:{},readTimeout:{},doOutput:{},doInput:{},requestMethod:{}}", httpUrl,connectTimeout,readTimeout,doOutput,doInput,requestMethod), e);
		}
	}
	
	/**
	 * 写出http流
	 * @param outString 写出的流内容
	 * @throws IOException 
	 */
	public void output(String outString) throws IOException{
		try {
			out = new DataOutputStream(connection.getOutputStream());
			out.writeBytes(outString);
			out.flush();
		} catch (IOException e) {
//			LogUtil.error(log,e,"写出的流内容异常，outString:{}",outString);
			throw new IOException(StringUtil.handleString("写出的流内容异常，outString:{}",outString), e);
		}
	}
	
	/**
	 * 读入http流
	 * @return StringBuilder
	 * @throws IOException 
	 */
	public StringBuilder input() throws IOException{
		StringBuilder inString = new StringBuilder();
		
		try {
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
			String line = null;
	
			while ((line = reader.readLine()) != null) {
				inString.append(line);
				inString.append("\r\n");
			}
		} catch (IOException e) {
//			LogUtil.error(log,e,"读入http流异常，inString:{}",inString);
			throw new IOException(StringUtil.handleString("读入http流异常，inString:{}",inString), e);
		}
		return inString;
	}
	
	/**
	 * 关闭http连接及相应的流 
	 * @throws IOException 
	 */
	public void close() throws IOException{
		try {
			if(out != null){
				out.close();
			}
			if(reader != null){
				reader.close();
			}
			if(connection != null){
				connection.disconnect();
			}
		} catch (IOException e) {
//			log.error("关闭http连接及相应的流异常",e);
			throw new IOException("关闭http连接及相应的流异常", e);
		}
	}
}
